<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList" :data="menuList" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border row-key="id"
                  style="width:100%;margin-bottom: 20px;"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="名称" prop="name"></el-table-column>
          <el-table-column label="图标" prop="picture">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.picture" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.picture" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="类别" prop="reward_type.name"></el-table-column>
          <!-- <el-table-column prop="active" label="状态">
               <template slot-scope="scope">
                   <el-tag type="success" v-if="scope.row.active === 1">启用</el-tag>
                   <el-tag type="danger" v-if="scope.row.active === 2">未启用</el-tag>
               </template>
           </el-table-column>
           <el-table-column prop="remark" label="备注"></el-table-column> -->

          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
            :limit.sync="listQuery.perPage"
            :page.sync="listQuery.page"
            :total="total"
            @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
        :visible.sync="deleteDialogVisible"
        title="确定删除吗?"
        width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible"
               @close="handleDialogClose('editRulesForm')"
    >
      <el-form ref="editRulesForm" :model="editRulesForm" :rules="editRulesFormRules" class="demo-ruleForm"
               label-width="120px"
      >
        <el-form-item label="道具名称" prop="name">
          <el-input v-model="editRulesForm.name" placeholder="请输入道具名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="道具描述" prop="remark">
            <el-input v-model="editRulesForm.remark" placeholder="请输入道具描述"></el-input>
        </el-form-item>-->
        <el-form-item label="道具分类" prop="type">
          <el-select v-model="editRulesForm.type" filterable placeholder="请选择">
            <el-option
                v-for="item in typeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 2" label="头像框" prop="type">
          <el-select v-model="editRulesForm.product_id" filterable placeholder="请选择">
            <el-option
                v-for="item in pendantType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 3" label="坐骑" prop="type">
          <el-select v-model="editRulesForm.product_id" filterable placeholder="请选择">
            <el-option
                v-for="item in mountType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 4" label="进场特效" prop="type">
          <el-select v-model="editRulesForm.product_id" filterable placeholder="请选择">
            <el-option
                v-for="item in specialType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editRulesForm.type === 6" label="礼物" prop="type">
          <el-select v-model="editRulesForm.product_id" filterable placeholder="请选择">
            <el-option
                v-for="item in giftType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图标" prop="picture">
          <el-upload
              ref="upload"
              :auto-upload="true"
              :file-list="specialFlagBigList"

              :on-change="handleChangePic"
              :on-remove="handleSpecialFlagBigRemove"
              :on-success="handleSpecialFlagBigSuccess"
              action=""
              class="upload-demo"
              list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传图标</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  listTypeData,
  putData,
  deleteData,
  listSelectData,
  listSpecialData,
  listGiftData
} from '@/api/prop/propList'
import {
  getToken
} from '@/utils/auth'
import { IMG_UPLOAD_URL } from '../../data/basic_data'

export default {
  data() {
    return {

      specialFlagBigList: [],
      typeOptions: [],

      giftType: [],
      mountType: [],
      pendantType: [],
      specialType: [],

      // 要修改的当前行的id
      row_id: 0,
      // 黑名单类别
      blackList: [
        { value: 1, label: '1' },
        { value: 2, label: '4' },
        { value: 3, label: '6' },
        { value: 4, label: '3' },
        { value: 5, label: '5' }
      ],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        //   remark:[{required:true,message:'请输入说明',trigger:'blur'}],
        type: [{ required: true, message: '请选择道具类别', trigger: 'change' }]
      },

      editRulesForm: {
        name: undefined,
        id: 0,
        picture: undefined,
        type: undefined,
        product_id: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
        type: undefined
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ''
    }
  },
  created() {
    this.getList()
    this.getTypeList()
    this.getSelectList()
    this.getSpecialList()
    this.getGiftList()
  },
  methods: {
    handleChangePic(fileList) {
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    handleSpecialFlagBigSuccess(res) {
      this.editRulesForm.picture = res.data.url
      this.specialFlagBigList = [{ url: res.data.url }]
    },
    handleSpecialFlagBigRemove(res) {
      this.editRulesForm.picture = undefined
      this.specialFlagBigList = []
    },
    getList() {
      this.listLoading = true
      let urlData = '?'
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`
      }
      if (this.listQuery.type) {
        urlData = urlData + `&type=${this.listQuery.type}`
      }
      listData(urlData).then((response) => {
        this.menuList = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getTypeList() {
      listTypeData(`?page=1&perPage=10000`).then(res => {
        res.data.data.forEach(item => {
          this.typeOptions.push({ value: item.id, label: item.name })
        })
      })
    },
    getSelectList() {
      listSelectData().then(response => {
        // response.data.gift.forEach(item => {
        //     this.giftType.push({value:item.id,label:item.name})
        // })
        response.data.ride.forEach(item => {
          this.mountType.push({ value: item.id, label: item.title })
        })
        response.data.head.forEach(item => {
          this.pendantType.push({ value: item.id, label: item.title })
        })
      })
    },
    getGiftList() {
      let obj = {
        page: 1,
        perPage: 100000
      }
      listGiftData(obj).then(response => {
        response.data.list.forEach(item => {
          this.giftType.push({ value: item.id, label: item.name })
        })
      })
    },
    getSpecialList() {
      let obj = {
        page: 1,
        perPage: 100000,
        category_id: 4
      }
      listSpecialData(obj).then(response => {
        response.data.list.forEach(item => {
          this.specialType.push({ value: item.id, label: item.title })
        })
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id
      this.editRulesForm.id = row.id
      this.editRulesForm.name = row.name
      this.editRulesForm.type = row.type
      // this.editRulesForm.remark = row.remark;
      this.editRulesForm.product_id = row.product_id
      this.editRulesForm.picture = row.picture
      this.specialFlagBigList = [{ url: row.picture }]

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          name: undefined,
          type: undefined,
          id: 0,
          picture: undefined,
          product_id: undefined
        }
        this.specialFlagBigList = []
      })

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let obj = {}
          for (let key in this.editRulesForm) {
            if (this.editRulesForm[key] !== undefined && this.editRulesForm[key] !== '' && this.editRulesForm[key] !== null) {
              obj[key] = this.editRulesForm[key]
            }
          }
          if (this.dialogStatus === 'create') {
            putData(obj).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          } else {
            putData(obj).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { id: row.id }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {

      deleteData(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.$message({
            message: response.message,
            type: 'error'
          })
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style scoped>
/deep/ .el-upload-list__item {
  transition : none !important;
}
</style>
